package com.kitty.game.task.service.targetHandler;

import com.kitty.game.role.model.Role;
import com.kitty.game.config.TaskSet;
import com.kitty.common.utils.SpringUtils;
import com.kitty.game.task.model.product.targetparam.OnEquipTargetParam;
import com.kitty.game.task.model.product.TaskTarget;
import com.kitty.game.task.model.product.TaskTargetType;
import com.kitty.game.task.service.NewTaskService;
import com.kitty.listener.BaseGameEvent;
import com.kitty.listener.event.OnEquipEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

public class OnEquipTargetHandler extends TaskTargetHandler {
    Logger logger = LoggerFactory.getLogger(getClass());


    @Override
    public void handleEvent(Role role, BaseGameEvent baseGameEvent) {
        OnEquipEvent onEquipEvent = (OnEquipEvent)baseGameEvent;
        NewTaskService taskService = SpringUtils.getBean(NewTaskService.class);
        List<TaskSet> taskSetList = taskService.getAcceptedTasksMatchTarget(role, TaskTargetType.ON_EQUIP);
        for (TaskSet taskSet : taskSetList) {
            TaskTarget doingTaskTarget = taskService.getRoleDoingTaskTarget(role, taskSet);
            /**任务目标都完成时，可能获得null*/
            if (doingTaskTarget == null) {continue ;}

//            if (!isTaskTargetMatchEquip(doingTaskTarget, onEquipEvent.getEquipName())) {continue; }

logger.trace("收到穿戴装备的事件，并且更新任务目标。uid: {}, equip:{} taskId: {}, index: {}", onEquipEvent.getPlayerUid(), onEquipEvent.getEquipName(), taskSet.getId(), doingTaskTarget.getTargetIndex());
            taskService.updateTargetProgress(role, taskSet, doingTaskTarget);
        }
    }

    @Override
    public void triggerNow(Role role, TaskSet taskSet, TaskTarget taskTarget) {

    }


    /**任务目标是否匹配装备*/
    private boolean isTaskTargetMatchEquip(TaskTarget taskTarget, String equipName) {
        return ((OnEquipTargetParam)taskTarget.getTaskTargetParam()).getEquips().contains(equipName);
    }
}
